<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="140px"
    >
      <el-form-item label="实验任务名称" prop="experimentName">
        <el-input v-model="dataForm.experimentName" placeholder="请填入实验任务名称"></el-input>
      </el-form-item>
      <el-form-item label="实验任务时长" prop="experimentDuration" >
        <el-input v-model="dataForm.experimentDuration" placeholder="请填入数字，默认单位分钟"></el-input>
      </el-form-item>
      <el-form-item label="实验任务类型" prop="testeeState">
<!--        <el-input v-model="dataForm.testeeState" ></el-input>-->
        <el-select v-model="dataForm.testeeState" placeholder="请选择任务状态">
          <el-option label="静息态" :value="0"></el-option>
          <el-option label="任务态" :value="1"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false;">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import task from '@/api/task'
export default {
  data() {
    return {
      visible:true,
      id:'',
      dataRule:{
        experimentName: [{ required: true, message: "实验任务名称不能为空", trigger: "blur" }],
        experimentDuration: [{ required: true, message: "实验任务时长不能为空", trigger: "blur" }],
        testeeState: [{ required: true, message: "实验任务类型不能为空", trigger: "blur" }],
        tag_mean:[{ required: true, message: "若无实验标签，填写’无’即可", trigger: "blur" }],
      },

      dataForm:{
        experimentName:"",


      }



    }

  },
  methods:{
    init(id)
    {
      this.dataForm={};
      this.dataForm.id = id;
      this.$refs["dataForm"].resetFields();
      if(id!=0)
      {
        task.getById(id).then((response)=>{
          if(response.code=='000000'){

            this.dataForm = response.data;


          }
          else{
            this.$message.error('获取数据失败');
          }
        })
      }
     // this.dataForm.id = id || 0;
      this.visible = true;
      this.dataForm.subjectPkId=this.$route.query.subjectPkId;
    },
    dataFormSubmit()
    {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {

          if(this.dataForm.id==0)//id为0就是更新啊
          {
            task.experiment(this.dataForm).then(({ code }) => {
              if (code  === '000000') {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 700,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList");
                  }
                });
              } else {
                this.$message.error(data.msg);
              }
            });
          }
          else{
            task.updateExperiment(this.dataForm).then(({ code }) => {
              if (code  === '000000') {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 700,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList");
                  }
                });
              } else {
                this.$message.error(data.msg);
              }
            });
          }
        }
      })
    }
  }
}
</script>
<style>

</style>
